﻿using System;
using System.Linq;
using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;

namespace Pay.Service {
	public class ShopService : ServiceBase, IShopService, IDisposable {
		private IUnitOfWork _iUnitOfWork;
		private IRegionService _iRegionService;

		public ShopService(IUnitOfWork iUnitOfWork, IRegionService iRegionService) : base(iUnitOfWork) {
			_iUnitOfWork = iUnitOfWork;
			_iRegionService = iRegionService;
		}

		public shop_dto GetByMerchant(long merchant_id) {
			var result = context.Shops.Where(c => c.MerchantId == merchant_id).FromCacheFirstOrDefault<Shop, shop_dto>("Shop");
			var region_ids = _iRegionService.GetRegionIds(result.area_id.Value);
			if (region_ids.Length == 3) {
				result.province_id = region_ids[0];
				result.city_id = region_ids[1];
				result.area_id = region_ids[2];
			} else if (region_ids.Length == 2) {
				result.province_id = region_ids[0];
				result.city_id = region_ids[1];
			} else if (region_ids.Length == 1) {
				result.province_id = region_ids[0];
			}
			return result;
		}

		public shop_dto GetByMerchantNo(string bind_no) {
			var result = context.Shops.Where(c => c.Merchant.MerchantNo == bind_no).FromCacheFirstOrDefault<Shop, shop_dto>("Shop" + bind_no);
			var region_ids = _iRegionService.GetRegionIds(result.area_id.Value);
			if (region_ids.Length == 3) {
				result.province_id = region_ids[0];
				result.city_id = region_ids[1];
				result.area_id = region_ids[2];
			} else if (region_ids.Length == 2) {
				result.province_id = region_ids[0];
				result.city_id = region_ids[1];
			} else if (region_ids.Length == 1) {
				result.province_id = region_ids[0];
			}
			return result;
		}

		public shop_dto Get(long id) {
			var result = context.Shops.Where(c => c.Id == id).FromCacheFirstOrDefault<Shop, shop_dto>("Shop");
			var region_ids = _iRegionService.GetRegionIds(result.area_id.Value);
			if (region_ids.Length == 3) {
				result.province_id = region_ids[0];
				result.city_id = region_ids[1];
				result.area_id = region_ids[2];
			} else if (region_ids.Length == 2) {
				result.province_id = region_ids[0];
				result.city_id = region_ids[1];
			} else if (region_ids.Length == 2) {
				result.province_id = region_ids[0];
			}
			return result;
		}

		public int Add(shop_dto dto) {
			var model = dto.ToDtoObject<Shop>();

			context.Shops.Add(model);
			int result = _iUnitOfWork.Commit("Shop");
			return result;
		}

		public int Update(shop_dto dto) {
			var model = context.Shops.FindById(dto.id);
			dto.ToDtoObject<Shop>(model);

			int result = _iUnitOfWork.Commit("Shop");
			return result;
		}

		public ApiResultMessage Save(shop_model dto) {
			if (dto.area_id.HasValue && dto.area_id == 0 && dto.city_id == 0)
				throw new ServerErrorException("城市没有选择");
			var model = context.Shops.FirstOrDefault(c => c.Merchant.MerchantNo == dto.bind_no);
			if (model == null) {
				var user = context.Merchants.FirstOrDefault(c => c.MerchantNo == dto.bind_no);
				if (user != null) {
					model = new Shop {
						MerchantId = user.Id, ShopName = dto.shop_name, Logo = dto.logo, Address = dto.address, Banner = dto.address, CreateDate = DateTime.Now.ToUnix(),
						Description = dto.description, SellerPhone = dto.seller_phone, RegionId = dto.area_id.HasValue && dto.area_id > 0 ? dto.area_id.Value : dto.city_id,
						IsInt = dto.is_int, InRate = dto.in_rate, OutRate = dto.out_rate
					};
					var region = _iRegionService.Get(model.RegionId);
					model.RegionName = region.region_name;
					model.Setting = new shop_setting { hf_set = new hf_native_set { category_wx = dto.category_wx, category_zfb = dto.category_zfb, wx_account = dto.wx_account } }.ToJson();
					context.Shops.Add(model);
				} else {
					throw new ServerErrorException("用户不存在");
				}
			} else {
				model.ShopName = dto.shop_name;
				model.Logo = dto.logo;
				model.Address = dto.address;
				model.Banner = dto.address;
				model.Description = dto.description;
				model.SellerPhone = dto.seller_phone;
				model.RegionId = dto.area_id.HasValue && dto.area_id > 0 ? dto.area_id.Value : dto.city_id;
				model.IsInt = dto.is_int; model.InRate = dto.in_rate; model.OutRate = dto.out_rate;
				var region = _iRegionService.Get(model.RegionId);
				model.RegionName = region.region_name;
				var setting = model.Setting.ToObject<shop_setting>();
				if (!string.IsNullOrEmpty(dto.category_wx))
					setting.hf_set.category_wx = dto.category_wx;
				if (!string.IsNullOrEmpty(dto.category_zfb))
					setting.hf_set.category_zfb = dto.category_zfb;
				if (!string.IsNullOrEmpty(dto.wx_account))
					setting.hf_set.wx_account = dto.wx_account;
				model.Setting = setting.ToJson();
			}

			int result = _iUnitOfWork.Commit("Shop" + dto.bind_no);
			return new ApiResultMessage("保存成功");
		}

		public int Delete(long id) {
			var model = context.Shops.FindById(id);
			context.Shops.Remove(model);
			int result = _iUnitOfWork.Commit("Shop");
			return result;
		}
	}
}
